package com.ganbo.stock.service.impl;

import com.ganbo.stock.entity.SuperBigDeal;
import com.ganbo.stock.enums.ServiceType;
import com.ganbo.stock.repository.SuperBigDealRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 大宗交易
 *
 * @Author: ganbo
 * @Date: 2020/12/28 19:28
 */

@Service
@Slf4j
public class SuperBigDealService extends BaseService {

    @Autowired
    SuperBigDealRepository superBigDealRepository;

    @Transactional
    public void fetchLastDealDaySuperBigDeal() {
        Date now = new Date();
        List<SuperBigDeal> superBigDealDBs = superBigDealRepository.findSuperBigDealByRecordDate(now);
        if (!CollectionUtils.isEmpty(superBigDealDBs)) return;

        Map<String, String> params = new HashMap<>();
        List<SuperBigDeal> superBigDeals = sendReqReturnList(ServiceType.SUPER_BIG_DEAL, params, SuperBigDeal.class);
        if (!CollectionUtils.isEmpty(superBigDeals)) {
            for (SuperBigDeal superBigDeal : superBigDeals) {
                superBigDeal.setRecordDate(now);
                superBigDealRepository.save(superBigDeal);
            }
        }
    }

}
